<?
/**
 * 数组转化为xml
 * Author		peace@warmy.cn
 * Create Date	2008-03-05
 * Version		2.1
 */
class WArray2Xml
{
    private $XMLtext; 
    private $tag;
    private $encoding;
    private $cdata_open 	= "<![CDATA[";
    private $cdata_close	= "]]>";
    private $xml_define;
    private $tab			= "\t";
    private $br				= "\r\n";
    public function __construct($tag,$encoding = 'utf-8',$cdata = true)
    {
    	$this->tag	= $tag;
    	$this->encoding	= $encoding;
    	if(!$cdata)
    	{
    		$this->cdata_open	= "";
    		$this->cdata_close	= "";
    	}
    	else 
    	{
    		$this->tab			= "\t";
    		$this->br			= "\r\n";
    	}
    	$this->xml_define		= "<?xml version=\"1.0\" encoding=\"{$this->encoding}\"?>{$this->br}";
    } 
    public function array2xml($array, $output=true) {
        $this->XMLtext="<{$this->tag}>{$this->br}"; 
        $this->array_transform($array); 
        $this->XMLtext .="</{$this->tag}>";
        if($output) 
        	return $this->XMLtext; 
        else 
        	return "";
    } 

    private function array_transform($array){ 
        static $Depth; 

        foreach($array as $key => $value){ 
            if(!is_array($value)){ 
                if(isset($Tabs))
            		unset($Tabs); 
                $Tabs	= "";
                for($i=1;$i<=$Depth+1;$i++) $Tabs .= "{$this->tab}"; 
                if(preg_match("/^[0-9]\$/",$key)) $key = "n$key"; 
                $this->XMLtext .= "$Tabs<$key>".$this->xml_strtr($value)."</$key>{$this->br}"; 
            } else { 
                $Depth += 1; 
                if(isset($Tabs))
            		unset($Tabs); 
                $Tabs	= "";
                for($i=1;$i<=$Depth;$i++) $Tabs .= "{$this->tab}"; 
                //search for atribut like [name]-ATTR to put atributs to some object 
                if(!preg_match("/(-ATTR)\$/", $key)) { 
                    if(preg_match("/^[0-9]\$/",$key)) $keyval = "n$key"; else $keyval = $key; 
                    $closekey = $keyval; 
                    if(isset($array[$key."-ATTR"]) && is_array($array[$key."-ATTR"])){ 
                        foreach ($array[$key."-ATTR"] as $atrkey => $atrval ) $keyval .= " ".$atrkey."=\"$atrval\""; 
                    } 
                    $this->XMLtext.="$Tabs<$keyval>{$this->br}"; 
                    $this->array_transform($value); 
                    $this->XMLtext.="$Tabs</$closekey>{$this->br}"; 
                    $Depth -= 1; 
                     
                } 
            } 
        } 
        return true; 
    }
    private function xml_strtr($value) 
    {
    	$retval	= "";
    	if(is_bool($value))
    	{
    		if($value)
    			$retval	= "1";
    		else 
    			$retval	= "0";
    	}
    	else
    	{ 
    		$retval	= $value;
    	}
    	$retval	= $this->cdata_open . $retval . $this->cdata_close;
    	return $retval;
    }
    protected function __get($key)
    {
    	if($key == "xml")
    		return $this->XMLtext;
    	else 
    		throw new Exception("Undefined property WArray2Xml::xml");
    }
}
?>